*-------------------------------------------------------------------------------
* Set the critical parameters of the computing environment
*-------------------------------------------------------------------------------
	clear all 
	macro drop _all
	
	set more off
	
	global	directory	`"/Volumes/GoogleDrive/My Drive/02_Stanford/00_Researching/16_SocialScientization/-01_SSH/00_replication/"'
	global	data		"${directory}00_data/"
	global	scripts		"${directory}01_scripts/"
	global	visuals		"${directory}02_visuals/"
	
	cd "${directory}"
	
*-------------------------------------------------------------------------------
* Read in Data
*------------------------------------------------------------------------------- 

	* Create independent variables
	
		do "${scripts}cr_uk_ivs.do"
	
	* Import parliamentary text data
	
		clear 
		import delimited "${data}uk_coded.csv"
				
		
		
	* Merge IVs
	
		merge m:1 year using "${data}uk_ivs.dta"
		drop if _merge == 2
		drop _merge
		
		
	* Gen decade
	
		cap drop decade
		egen decade = cut(year), at(1800(10)1920)
		
		
		
	* Generate year and decade tags
	
		cap drop yr_tag 
		egen yr_tag = tag(year)
		
		cap drop dec_tag
		egen dec_tag = tag(decade)
		
		
		
	* Generate n per year 
	
		cap drop n_year
		bysort year: gen n_year = _N
		
		
		
	* Generate commons indicator
	
		cap drop	commons
		gen 		commons = 1 if chamber == "lower"
		replace 	commons = 0 if chamber == "upper"
		
		
		
	* Year and length cutoff
	
		global	year 	= 1910
		drop if year	> $year
		
		
		
	* Encode PM var
	
		replace 	uk_hog_name = "William Pitt the Younger" ///
					if uk_hog_name == `"William Pitt "the Younger""'
					
		lab def		pms_lbl ///
					1 "Henry Addington" ///
					2 "William Pitt the Younger" ///
					3 "William Wyndham Grenville" ///
					4 "William Henry Cavendish-Bentinck" ///
					5 "Spencer Perceval" ///
					6 "Robert Banks Jenkinson" ///
					7 "Frederick John Robinson" ///
					8 "Arthur Wellesley" ///
					9 "Charles Grey" ///
					10 "Robert Peel" ///
					11 "William Lamb" ///
					12 "John Russell" ///
					13 "George Hamilton-Gordon" ///
					14 "Henry John Temple" ///
					15 "Edward Geoffrey Stanley" ///
					16 "William Ewart Gladstone" ///
					17 "Benjamin Disraeli" ///
					18 "Robert Arthur Talbot Gascoyne-Cecil" ///
					19 "Archibald Primrose" ///
					20 "Arthur James Balfour" ///
					21 "Henry Campbell-Bannerman" ///
					22 "Herbert Henry Asquith", replace
					
		cap drop pms			
		encode uk_hog_name, gen(pms) label(pms)
		
								
				
*-------------------------------------------------------------------------------
* Schooling keywords
*-------------------------------------------------------------------------------  
								
						
	global	sch 	sch_educ ///
					sch_school ///
					sch_instruct ///
					sch_teacher ///
					sch_teach ///
					sch_train ///
					sch_scholar ///
					sch_child ///
					sch_pupil

	
	* Conduct FA
	
		cap drop schooling
		factor $sch, ipf 
		rotate
		predict schooling
		
		cap drop schooling_std
		egen schooling_std = std(schooling)
		
		
			
*-------------------------------------------------------------------------------
* Create Lagged Year Measures 
*-------------------------------------------------------------------------------  
		
	preserve
	
	
		cap drop	schooling_year
		egen		schooling_year 	= mean(schooling_std), by(year)		
		
		
		
	* keep only year obs. 
	
		keep if yr_tag == 1
		global lag = 1 // lag by one year
		
		

		
	* gen growth rates
	
		gen uk_colonies_grow = (uk_colonies - uk_colonies[_n-1]) ///
								/ uk_colonies[_n-1]
												
		gen uk_urbnztn_grow = (uk_urbnztn - uk_urbnztn[_n-1]) / uk_urbnztn[_n-1]
			
			
			
			
	* transform rates into %
	
		replace uk_colonies_grow = uk_colonies_grow * 100
		replace uk_gdpgrow = uk_gdpgrow * 100
		replace uk_urbnztn_grow = uk_urbnztn_grow * 100
		replace uk_suffrage = uk_suffrage * 100
		
		
		
	* Create lagged dependent variable
	* last year's average
	
		cap drop 	nl_schooling_std		
		gen nl_schooling_std = schooling_year[_n-$lag]
	
		
		
		*-----------------------------------
		* UK functionalist explanations
		*-----------------------------------
		
			global	ukfnctnlst	uk_suffrage ///
								uk_int_cnflct ///
								uk_dmst_cnflct ///
								uk_gdppc ///
								uk_urbnztn_grow ///
								uk_gdpgrow ///
								uk_colonies_grow
							
							
							
			* Create lagged variables
				
				foreach var in $ukfnctnlst {
					cap drop nl_`var'
					gen nl_`var' = `var'[_n-$lag]
					cap drop std_`var
					egen nl_std_`var' = std(nl_`var')
					}
					
					
			* Create factor of development explanations
					
				factor uk_gdpgrow uk_urbnztn_grow uk_suffrage, factor(1)  
				rotate
				cap drop development
				predict development 
				
				cap drop nl_development
				gen nl_development = development[_n-1]
				
				cap drop nl_development_std
				egen nl_development_std = std(nl_development)
							
					
					
		*-----------------------------------			
		* Continental affairs
		*-----------------------------------
		
			
			gen nl_wst_dmst_cnflct = wst_dmst_cnflct[_n-$lag]
			egen nl_std_wst_dmst_cnflct = std(nl_wst_dmst_cnflct)
			

			
		*-----------------------------------			
		* Scientization
		*-----------------------------------
		
			global scientization		wst_census_ever ///
										wst_agency ///
										wst_yrbkpub ///
										uk_yrbkpub ///
										uk_census_all ///
										journals ///
										fellows ///
										int_meetings ///
										uk_agency ///
										committees 
					

				cap drop scientization
				factor $scientization  ,  ipf factor(1)
				predict scientization  
				
				cap drop nl_scientization_std
				egen nl_scientization_std = std(scientization)
				
				su nl_scientization_std
				
				gen nl_scientization = nl_scientization_std[_n-$lag]
				
				
		* Save 

			keep year nl_*
			save "${data}lagged", replace
		
	restore
	
	
	
	* Merge with lagged measures
	
		cap drop _merge
		cap drop  nl_*
		merge m:1 year using "${data}lagged"
		cap erase "${data}lagged.dta"
		
		
		
	* Test if lagging worked
	
		egen	schooling_year = mean(schooling_std), by(year)
	
		assert 	schooling_year[_n-1] == nl_schooling_std if yr_tag == 1
		assert 	uk_dmst_cnflct[_n-1] == nl_uk_dmst_cnflct if yr_tag == 1
		assert	wst_dmst_cnflct[_n-1] == nl_wst_dmst_cnflct if yr_tag == 1
				
				
				
				
*-------------------------------------------------------------------------------
* Create SPEECH-LEVEL INDICATOR OF SCIENTIZATION 
*-------------------------------------------------------------------------------

		global	sci		sci_statist ///
						sci_number ///
						sci_show ///
						sci_evid ///
						sci_mean ///
						sci_popul ///
						sci_averag ///
						sci_figur ///
						sci_fact ///
						sci_estim
						
						
		alpha $sci, std item


		factor $sci if year < $year, ipf fact(1)
		rotate
		predict science
		
		cap drop sci_terms_std
		egen sci_terms_std = std(science)	
		
		
		
		
		
							
*-------------------------------------------------------------------------------
* Table 2 — Models
*-------------------------------------------------------------------------------  		

		cap drop miss
		egen miss = rowmiss(schooling_std ///
											nl_scientization	///
											nl_schooling_std	///
											length				///
											commons				///
											pms 				///
											nl_uk_dmst_cnflct   ///
											nl_wst_dmst_cnflct	///
											nl_uk_colonies_grow	///
											nl_uk_gdpgrow 		///
											nl_uk_urbnztn_grow 	///
											nl_uk_suffrage		///
											nl_development_std	///
											sci_terms_std)
		
		
		preserve 
			global	directory	`"/Volumes/GoogleDrive/My Drive/02_Stanford/00_Researching/16_SocialScientization/-01_SSH/00_replication/"'
			global	data		"${directory}00_data/"
			global	scripts		"${directory}01_scripts/"
			global	visuals		"${directory}02_visuals/"
			cd "${directory}"


			drop if year > $year 
			drop if miss > 0
			
			gen _year = year-1803
			drop year
			rename _year year
			
			
			global excel "${visuals}table2.xls"
			global txt "${visuals}table2.txt"

			
			cap erase "${excel}"
			cap erase "${txt}"
			
			
			 outreg2 using "${excel}", dec(3) excel: ///
			 reg schooling_std 	nl_scientization	///
								, robust 	
								
							
							
			
			 outreg2 using "${excel}", dec(3) excel: ///
			 reg schooling_std 	nl_scientization	///
								year 			///
								length				///
								commons	 			///
								, robust  	
								 
								
			
			 outreg2 using "${excel}", dec(3) excel: ///
			 reg schooling_std 	nl_scientization	///
								nl_schooling_std	///
								year 			///
								length				///
								commons	 			///
								, robust  	
								 
			
			 outreg2 using "${excel}", dec(3) excel: ///
			 reg schooling_std 	nl_scientization	 sci_terms_std ///
								nl_schooling_std	///
								year 			///
								length				///
								commons	 			///
								, robust  	
								 
								
			
			 outreg2 using "${excel}", dec(3) excel: ///
			 reg schooling_std 	nl_scientization	sci_terms_std ///
								nl_schooling_std	///
								year  ///
								length				///
								commons				///
								nl_uk_dmst_cnflct   ///
								nl_wst_dmst_cnflct	///
								, robust  	
								 
			 outreg2 using "${excel}", dec(3) excel: ///
			 reg schooling_std 	nl_scientization	sci_terms_std ///
								nl_schooling_std	///
								year 			///
								length				///
								commons				///
								nl_uk_dmst_cnflct   ///
								nl_wst_dmst_cnflct	///
								nl_uk_colonies_grow	///
								, robust  	
								 
		
		
			global M6 			quietly:	reg schooling_std 	sci_terms_std ///
												nl_scientization	///
												nl_schooling_std	///
												year 			///
												length				///
												commons				///
												nl_uk_dmst_cnflct   ///
												nl_wst_dmst_cnflct	///
												nl_uk_colonies_grow	///
												, robust  	
				
		
			 global 	dev 			///
						nl_uk_gdpgrow 	///
						nl_uk_urbnztn_grow 	///
						nl_uk_suffrage ///
						nl_development_std
			 
			 
			 
			 foreach var in $dev { 
				 $M6 // re-run M5 to get adj R2
				 outreg2 using "${excel}", dec(3) excel: ///
				 reg schooling_std 	nl_scientization	sci_terms_std ///
									nl_schooling_std	///
									length				///
									commons				///
									year			///
									nl_uk_dmst_cnflct   ///
									nl_wst_dmst_cnflct	///
									nl_uk_colonies_grow		///
									`var'				///
									, robust  	
				}		
		
		cap erase "${txt}"
		restore
		

		
*-------------------------------------------------------------------------------
* Table 3 — Wald F Tests of Composite Linear Hypotheses
*------------------------------------------------------------------------------- 
	preserve 	
	cd "${visuals}"

		drop if year > 1909 
		drop if miss > 0
				 
		 
		reg schooling_std 	nl_scientization	sci_terms_std ///
				nl_schooling_std	///
				length				///
				commons				///
				i.decade			///
				nl_std_uk_dmst_cnflct   ///
				nl_std_wst_dmst_cnflct	///
				nl_std_uk_colonies_grow	///
				 , robust
				 
		asdoc test nl_scientization = nl_std_uk_dmst_cnflct, save(table3.doc)
		asdoc test nl_scientization = nl_std_wst_dmst_cnflct, save(table3.doc)
		asdoc test nl_scientization = nl_std_uk_colonies_grow, save(table3.doc)

		 global 	dev 			///
					nl_std_uk_gdpgrow 	///
					nl_std_uk_urbnztn_grow 	///
					nl_std_uk_suffrage ///
					nl_development
				

		
		 foreach var in $dev {	 
		 	reg schooling_std 	nl_scientization	///
								nl_schooling_std	///
								length				///
								commons				///
								pms 				///
								i.decade			///
								nl_std_uk_dmst_cnflct   ///
								nl_std_wst_dmst_cnflct	///
								nl_std_uk_colonies_grow	///
								`var'				///
								 , robust
		 di " "
		 asdoc test nl_scientization = `var', save(table3.doc)
		 }
		 
		  
		 
	restore
	
	
	
*-------------------------------------------------------------------------------
* Produce visuals
*------------------------------------------------------------------------------- 
	global	directory	`"/Volumes/GoogleDrive/My Drive/02_Stanford/00_Researching/16_SocialScientization/-01_SSH/00_replication/"'
	global	data		"${directory}00_data/"
	global	scripts		"${directory}01_scripts/"
	global	visuals		"${directory}02_visuals/"

	
	cd "${directory}"

	quietly do "${scripts}ssh_figures.do"
	
	erase "${data}uk_ivs.dta"
